package com.xbai.user.controller;

import com.github.pagehelper.PageInfo;
import com.xbai.entity.Result;
import com.xbai.user.pojo.Provinces;
import com.xbai.user.service.ProvincesService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc ProvincesController
 * @author jxbai
 * @date 2021/1/28
 */

@RestController
@RequestMapping("/provinces")
@CrossOrigin
@RequiredArgsConstructor
public class ProvincesController {

    private final ProvincesService provincesService;

    /***
     * Provinces分页条件搜索实现
     * @param provinces {@link Provinces}
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) Provinces provinces, @PathVariable  int page, @PathVariable  int size){
        //调用ProvincesService实现分页条件查询Provinces
        PageInfo<Provinces> pageInfo = provincesService.findPage(provinces, page, size);
        return new Result<>(pageInfo);
    }

    /***
     * Provinces分页搜索实现
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用ProvincesService实现分页查询Provinces
        PageInfo<Provinces> pageInfo = provincesService.findPage(page, size);
        return new Result<>(pageInfo);
    }

    /***
     * 多条件搜索Provinces数据
     * @param provinces {@link Provinces}
     * @return {@link Result}
     */
    @PostMapping(value = "/search" )
    public Result<List<Provinces>> findList(@RequestBody(required = false) Provinces provinces){
        //调用ProvincesService实现条件查询Provinces
        List<Provinces> list = provincesService.findList(provinces);
        return new Result<>(list);
    }

    /***
     * 根据ID删除Provinces数据
     * @param id 主键id
     * @return {@link Result}
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用ProvincesService实现根据主键删除
        provincesService.delete(id);
        return new Result();
    }

    /***
     * 修改Provinces数据
     * @param provinces {@link Provinces}
     * @param id 主键id
     * @return {@link Result}
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Provinces provinces, @PathVariable String id){
        //设置主键值
        provinces.setProvinceid(id);
        //调用ProvincesService实现修改Provinces
        provincesService.update(provinces);
        return new Result();
    }

    /***
     * 新增Provinces数据
     * @param provinces {@link Provinces}
     * @return {@link Result}
     */
    @PostMapping
    public Result add(@RequestBody Provinces provinces){
        //调用ProvincesService实现添加Provinces
        provincesService.add(provinces);
        return new Result();
    }

    /***
     * 根据ID查询Provinces数据
     * @param id 主键id
     * @return {@link Result}
     */
    @GetMapping("/{id}")
    public Result<Provinces> findById(@PathVariable String id){
        //调用ProvincesService实现根据主键查询Provinces
        Provinces provinces = provincesService.findById(id);
        return new Result<>(provinces);
    }

    /***
     * 查询Provinces全部数据
     * @return {@link Result}
     */
    @GetMapping
    public Result<List<Provinces>> findAll(){
        //调用ProvincesService实现查询所有Provinces
        List<Provinces> list = provincesService.findAll();
        return new Result<>(list) ;
    }
}
